﻿using KTD.Web.Models;
using KTD.Web.Models.VD;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using XXS.Common.BaseDataAccessLayer;
using XXS.Common.Paginate;

namespace KTD.Web.BLL
{
    public class CommentManager
    {
        private readonly IBaseDAL<Comment> _CommentDal;
        public CommentManager()
        {
            _CommentDal = new BaseDAL<Comment>(new DB());
        }

        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="im"></param>
        /// <returns></returns>
        public virtual IPaginate<CommentVD> GetListBy(CommentQD im)
        {
            var res = _CommentDal.FindList();
            if (!string.IsNullOrEmpty(im.Query))
            {
                res = res.Where(n =>  n.content.Contains(im.Query));
            }
            if (im.HouseId!=0)
            {
                res = res.Where(n =>n.hid==im.HouseId);
            }
            var list = res.OrderByDescending(n => n.CreateDateTime)
                .ToPaginate(im,n => new CommentVD().MapFrom(n));
            return list;
        }


        public void Create(CommentCD im)
        {
            Comment model = new Comment();
            im.MapTo(model);
            _CommentDal.Create(model);
        }

      

        public CommentCD Find(int id)
        {
            Comment model = _CommentDal.Find(id);
            return new CommentCD().MapFrom(model);
        }

        public void Delete(int id)
        {
            Comment model = _CommentDal.Find(id);
            if (model == null)
            {
                throw new Exception("错误的ID");
            }
            _CommentDal.Delete(model);
        }
    }
}